$(function () {
    $("#jqGrid").jqGrid({
        url: '/platform/manage/payAccessList',
        datatype: "json",
        colModel: [
			{ label: 'ID', name: 'payAccessId', index: 'payAccessId',key:true,hidden:true},
			{ label: '使用通道', name: 'accessName', index: 'accessName', },
			{ label: '单笔(元)', name: 'singleTime', index: 'singleTime', },
            { label: '单卡(元)', name: 'singleCard', index: 'singleCard', },
            { label: '单用户(元)', name: 'singleUser', index: 'singleUser', },
            { label: '费率', name: 'rate', index: 'rate', },
            { label: '网关成本', name: 'gatewayCost', index: 'gatewayCost', },
            { label: '清算费率', name: 'clearRate', index: 'clearRate',  },
            { label: '清算费', name: 'clearMoney', index: 'clearMoney', },
            { label: '最低交易金额', name: 'minMoney', index: 'minMoney', },
            { label: '提现费(元)', name: 'deposit', index: 'deposit',  },
			{ label: '开始时间', name: 'transStart', index: 'transStart', },
            { label: '结束时间', name: 'transEnd', index: 'transEnd', },
            { label: '通道状态', name: 'payStatus', index: 'payStatus',formatter:function (value) {
				return value == '1' ? '<label class="label label-success">上线</label>':'<label class="label label-danger">下架</label>';
            } }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "result.list",
            page: "result.currPage",
            total: "result.totalPage",
            records: "result.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条\
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.loadAccess();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		showInfo:false,
		title: null,
		isUpdate:false,
		access: {accessId:0},
		accesses:[]
	},
	methods: {
		loadAccess:function () {
			$.getJSON("/platform/manage/payAllAccessList",null,function (r) {
				if (r.code == 200)
					vm.accesses = r.result;
				else
					alert(r.message);
            })
        },
		add:function () {
            vm.showList = false;
            vm.showInfo = true;
            vm.title = "新增";
            vm.access = {accessId:0};
            vm.isUpdate = false;
        },
		update: function (event) {
			var appGradeId = getSelectedRow();
			if(appGradeId == null){
				return ;
			}
            // vm.app = $("#jqGrid").jqGrid().getRowData(appGradeId);
            vm.showList = false;
            vm.showInfo = true;
            vm.title = "修改";
            vm.getInfo(appGradeId);
            vm.isUpdate = true;
		},
        saveOrUpdate: function (event) {
			console.log("accessId",vm.access);
			if (vm.access.accessId == 0 || vm.access.accessId == null){
				alert("请选择通道");
				return;
			}
			var url =  vm.isUpdate ? "/platform/manage/payAccessUpdate" : "/platform/manage/payAccessAdd";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.access),
			    success: function(r){
			    	if(r.code == 200){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.message);
					}
				}
			});
		},
		getInfo: function(id){
			$.get("/platform/manage/payAccessInfo/"+id, function(r){
				if (r.code != 200){
				    alert(r.message);
                }else {
                    vm.access = r.result;
				}
            });
		},
        reload: function (event) {
            vm.showList = true;
            vm.showInfo = false;
			$("#jqGrid").jqGrid('setGridParam',{
                page:1
            }).trigger("reloadGrid");
            vm.isUpdate = false;
		}
	}
});